#!/bin/bash

source /etc/sysconfig/agora  > /dev/null
source /usr/local/share/agora/current/services/agora/agora.sh > /dev/null






#---~~---.~---~~---.~---~~---.~---~~---.~---~~---.~---~~---.~---~~---.~---~~---+

function agoraDCOP()

#---~~---.~---~~---.~---~~---.~---~~---.~---~~---.~---~~---.~---~~---.~---~~---+

{
echo "dentro do AGORADCOP com ${1}, ${2} e ${3} " >> /tmp/gsd.log

   local    ThisGroup="${1}" ;
   local   ThisMember="${2}" ;
   local TargetMember="${3}" ;

   local DelimiterDCOP="__D__"


# semaphore
   local    SemaphoreName="semaphore_${FUNCNAME}" ;
   local        TimerName="timer_${TargetMember}" ;
   local TimerDurationSec="5"                     ;
   
#   miscSemaphoreUp   "${ThisGroup}"  "${ThisMember}"  "${SemaphoreName}"  "${TimerName}" "${TimerDurationSec}"



#   unset    DCOPSERVER
#   DCOPAUTHORITY_BAK=$DCOPAUTHORITY
#    ICEAUTHORITY_BAK=$ICEAUTHORITY

   export   DCOPAUTHORITY=${AGORA_HOME_DIR}/groups/${ThisGroup}/Members/${TargetMember}/view/.dcop_auth
   export    ICEAUTHORITY=${AGORA_HOME_DIR}/groups/${ThisGroup}/Members/${TargetMember}/view/.ice_auth


#   DCOPAUTHORITY=${AGORA_HOME_DIR}/groups/${ThisGroup}/Members/${TargetMember}/view/.dcop_auth
#   ICEAUTHORITY=${AGORA_HOME_DIR}/groups/${ThisGroup}/Members/${TargetMember}/view/.ice_auth

	echo "chamou dcop klauncher klauncher kdeinit_exec /usr/local/share/agora/current/services/agora/agoraView.sh ${DelimiterDCOP} ${ThisGroup} ${ThisMember} ${TargetMember} ${DelimiterDCOP} 0 0" >> /tmp/gsd.log

   dcop klauncher klauncher kdeinit_exec /usr/local/share/agora/current/services/agora/agoraView.sh ${DelimiterDCOP} "${ThisGroup}" "${ThisMember}" "${TargetMember}" ${DelimiterDCOP} 0 0 2> /tmp/dcoperr.log

   unset DCOPAUTHORITY
   unset ICEAUTHORITY

#DCOPAUTHORITY=$DCOPAUTHORITY_BAK
#ICEAUTHORITY=$ICEAUTHORITY_BAK



#   miscSemaphoreDown "${ThisGroup}" "${ThisMember}" "${SemaphoreName}" "${TimerName}"


}




















# TODO: finir de v�rifier cette fonction

function emsServiceAddButton()

{
   local    ThisGroup="${1}" ;
   local   ThisMember="${2}" ;
   local TargetMember="${3}" ;
   local   ServiceURI="${4}" ;

   # TODO v�rifier que ThisMember est EMS manager

   DCOPAUTHORITY=${AGORA_HOME_DIR}/groups/${ThisGroup}/Members/${TargetMember}/view/.dcop_auth
    ICEAUTHORITY=${AGORA_HOME_DIR}/groups/${ThisGroup}/Members/${TargetMember}/view/.ice_auth

   dcop kicker Panel addURLButton "${ServiceURI}"

}





























function broadcast_message()
{

   MESSAGE=${1};
   declare -a USER_LIST=`cat /etc/group | grep "${SELECTED_GROUP}:" | cut -d : -f 4 | sed s/,/\ /g`;

   for i in ${USER_LIST[@]} ; do
      dcop --user ${i} --all-sessions knotify default notify eventname appname ${MESSAGE} '' '' 2 0;
   done

}




function broadcast_document()
{
   DOCUMENT=${1};
   declare -a USER_LIST=`cat /etc/group | grep "${SELECTED_GROUP}:" | cut -d : -f 4 | sed s/,/\ /g`;

   for i in ${USER_LIST[@]} ; do
      # identifie une seule instance de konqueror avec l'option grep -m
      KONQ_INSTANCE=dcop --user ${i} | grep konqueror -m 1;
      if [ -n ${KONQ_INSTANCE} ]; then
        dcop --user ${i} --all-sessions  ${KONQ_INSTANCE} default notify eventname appname ${MESSAGE} '' '' 2 0;
      fi
   done

}




#--------------------------------------------
# test des fonctions

# broadcast_message "Notification envoy�e par $USER: juste un essai"



# Exemples utiles

# Identifie une instance unique
# KONQ_INSTANCE=dcop --user azerty | grep konqueror -m 1;
# Noter que dans "konqueror-NNNNN," "NNNNN" correspond au pid.

# Renvoi true si la fenetre de l'instance konqueror a le focus
# dcop --user azerty ${KONQ_INSTANCE} konqueror-mainwindow#1 focus

# Ouvre une fen�tre chez l'utilisateur azerty
# dcop --user azerty ${KONQ_INSTANCE} KonquerorIface openBrowserWindow http://www.google.fr

# Methode alternative en 2 temps

# KONQ_WINDOW=dcop --user azerty ${KONQ_INSTANCE} | grep konqueror -m 1;
#             retourne  konqueror-mainwindow#XXX avec x entier
# dcop --user azerty ${KONQ_INSTANCE} ${KONQ_WINDOW} openURL http://www.lirmm.fr
# dcop --user azerty ${KONQ_INSTANCE} ${KONQ_WINDOW} show

# mode plein ecran
# dcop --user azerty ${KONQ_INSTANCE} ${KONQ_WINDOW} action fullscreen
# Renvoi la description de l'action

# dcop --user azerty `dcop --user azerty ${KONQ_INSTANCE} ${KONQ_WINDOW} action fullscreen`
# Renvoi les fonctions relatives au mode plein �cran, il faut ensuite l'activer

# Pour activer le mode plein �cran
# dcop --user azerty `dcop --user azerty ${KONQ_INSTANCE} ${KONQ_WINDOW} action fullscreen` activate


# Pour ajouter des applis sur la barre du bureau
# OPEN_TERM=0
# dcop --user azerty kicker Panel addNonKDEAppButton vncviewer "iconvnc" :1 $OPEN_TERM
